package com.zhku.laosiji.controller;

import com.zhku.laosiji.pojo.Park;
import com.zhku.laosiji.pojo.User;
import com.zhku.laosiji.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Jiayuan
 * @version 1.0
 * @description:
 * @time 9/11/2018 9:06 AM
 */

@Controller
@RequestMapping("/park")
public class ParkController {

    @Autowired
    private ParkService parkService;

    //查询所有车位,按条件筛选车位
    @RequestMapping("/find")
    public ModelAndView find(@RequestParam(value="status",required = false,defaultValue = "-1") Integer status,
							 Map<String,Object> map, HttpSession session){
			User user = ((User)session.getAttribute("user"));
			List<Park> parks = parkService.find(status);
			map.put("parks",parks);
			map.put("status",status);
			if(user != null){
				map.put("user",user);
			}
			return new ModelAndView("/user/car/car-select",map);
    }

    //查询车位详细信息
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("parkId")Integer id,Map<String,Object> map,
							   HttpSession session){
        Park park = parkService.findDetail(id);
        map.put("park",park);
        if(session.getAttribute("user") != null){
        	map.put("user",((User)session.getAttribute("user")));
		}
        return new ModelAndView("/user/car/detail",map);
    }

	
	@RequestMapping("/list")
	public ModelAndView selectAllPark() {
		List<Park> allPark = parkService.selectAllPark();
		int totalPark = parkService.countTotalPark();
		ModelAndView modelAndView = new ModelAndView("/park-list");
		modelAndView.addObject("allPark", allPark);
		modelAndView.addObject("totalPark", totalPark);
		return modelAndView;
	}
	
	@RequestMapping("/add")
	public ModelAndView toParkAdd() {
		return new ModelAndView("/park-add");
	}
	
	@RequestMapping("/park-add")
	public String addPark(Park record) {
		Park park = parkService.selectByPrimaryKey(record.getId());
		if(park == null) {
			parkService.insertSelective(record);
		}
		parkService.updateByPrimaryKey(record);
		return "redirect:/park/list";
	}
	
	@RequestMapping("/update")
	public ModelAndView toParkUpdate(Integer id) {
		Park park = parkService.selectByPrimaryKey(id);
		ModelAndView modelAndView = new ModelAndView("/park-add");
		modelAndView.addObject("id", park.getId());
		modelAndView.addObject("name", park.getName());
		modelAndView.addObject("price", park.getPrice());
		modelAndView.addObject("status", park.getStatus());
		return modelAndView;
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String delPark(Integer id) {
		parkService.deleteByPrimaryKey(id);
		return "OK";
	}
	
	@RequestMapping("/search")
	public ModelAndView searchPark(String parkname) {
		List<Park> allPark = parkService.selectParkByName(parkname);
		int totalPark = parkService.countSearchPark(parkname);
		ModelAndView modelAndView = new ModelAndView("/park-list");
		modelAndView.addObject("allPark", allPark);
		modelAndView.addObject("totalPark", totalPark);
		return modelAndView;
	}
	
}
